﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HT.Data.BussinessLogic;
using System.Data;
namespace Hotel.UserControls
{
    public partial class Guest : System.Web.UI.UserControl
    {
        public int GuestID
        {
            get;
            set;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GuestID = 0;
                pnlAdd.Visible = false;
                //LoadGuests();
                GetGuestList();
            }
        }

        private void LoadGuests()
        {
            DataSet ds = HotelManger.GetInstance().GetAllGuest(GuestID);
            gvGuest.DataSource = ds;
            gvGuest.DataBind();

        }
        public void LoadData()
        {

            DataSet ds = HotelManger.GetInstance().GetAllGuest(GuestID);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    FirstNameTextBox.Text = ds.Tables[0].Rows[0]["FirstName"].ToString();
                    LastNameTextBox.Text = ds.Tables[0].Rows[0]["LastName"].ToString();
                    BirthDateTextBox.Text = ds.Tables[0].Rows[0]["BirthDate"].ToString();
                    AgeTextBox.Text = ds.Tables[0].Rows[0]["Age"].ToString();
                    GenderTextBox.Text = ds.Tables[0].Rows[0]["Gender"].ToString();
                    NationalityTextBox.Text = ds.Tables[0].Rows[0]["Nationality"].ToString();
                    EmailTextBox.Text = ds.Tables[0].Rows[0]["Email"].ToString();
                    CountryCodeTextBox.Text = ds.Tables[0].Rows[0]["CountryCode"].ToString();
                    PhoneTextBox.Text = ds.Tables[0].Rows[0]["Phone"].ToString();
                    OccupationTextBox.Text = ds.Tables[0].Rows[0]["Occupation"].ToString();
                    AddressTextBox.Text = ds.Tables[0].Rows[0]["Address"].ToString();
                   
                    ChkProof.Checked = Convert.ToBoolean(ds.Tables[0].Rows[0]["hasProof"].ToString());
                    FirstNameTextBox.Text = ds.Tables[0].Rows[0]["FirstName"].ToString();
                    txtPassportNo.Text = ds.Tables[0].Rows[0]["passportnumber"].ToString();
                    

                }
            }


        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //LoadGuests();
            hdnMode.Value = "New";
            lblMessage.Text = "Add Guest";
            pnlView.Visible = false;
            pnlAdd.Visible = true;
        }

        protected void lnkEdit_Command(object sender, CommandEventArgs e)
        {
            GuestID = Convert.ToInt32(e.CommandName);
            hdnMode.Value = "Edit";
            lblMessage.Text = "Edit Guest";
            hdnGuestID.Value = GuestID.ToString();
            pnlView.Visible = false;
            pnlAdd.Visible = true;
            LoadData();

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

            pnlView.Visible = true;
            pnlAdd.Visible = false;
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            HT.Data.Objects.Guest guest = new HT.Data.Objects.Guest();
            guest.FirstName = FirstNameTextBox.Text.Trim();
            guest.LastName = LastNameTextBox.Text;
            guest.BirthDate = BirthDateTextBox.Text;
            guest.Age = (AgeTextBox.Text == "") ? 0 : Convert.ToInt32(AgeTextBox.Text);
            guest.Gender = GenderTextBox.Text;
            guest.Nationality = NationalityTextBox.Text;
            guest.Email = EmailTextBox.Text;
            guest.CountryCode = (CountryCodeTextBox.Text == "") ? 0 : Convert.ToInt32(CountryCodeTextBox.Text);
            guest.Phone = PhoneTextBox.Text;
            guest.Occupation = OccupationTextBox.Text;
            guest.Address = AddressTextBox.Text;

            guest.HasProof = ChkProof.Checked;
            guest.PassportNumber = txtPassportNo.Text;

            if (hdnMode.Value == "New")
                HotelManger.GetInstance().SaveGuest(guest);
            else if (hdnMode.Value == "Edit")
            {
                guest.GuestID = Convert.ToInt32(hdnGuestID.Value);
                guest.ModifiedBy = 0;
                HotelManger.GetInstance().UpdateGuest(guest);

            }
            LoadGuests();
            pnlView.Visible = true;
            pnlAdd.Visible = false;
            ResetFormControlValues(this);
        }

        private void ResetFormControlValues(Control parent)
        {
            foreach (Control c in parent.Controls)
            {
                if (c.Controls.Count > 0)
                {
                    ResetFormControlValues(c);
                }
                else
                {
                    switch (c.GetType().ToString())
                    {
                        case "System.Web.UI.WebControls.TextBox":
                            ((TextBox)c).Text = "";
                            break;
                        case "System.Web.UI.WebControls.CheckBox":
                            ((CheckBox)c).Checked = false;
                            break;
                        case "System.Web.UI.WebControls.RadioButton":
                            ((RadioButton)c).Checked = false;
                            break;

                    }
                }
            }
        }


        private void GetGuestList()
        {
            gvGuest.DataSource = HotelManger.GetInstance().GetAllGuestBySearch(FirstTextBox.Text.Trim(), LastTextBox.Text.Trim(), PhoneTxtBox.Text.Trim(), EmailTxtBox.Text.Trim());

            gvGuest.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GetGuestList();
        }
    }
}